package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.DistributionSemenCtlr;
import org.gov.vis.data.controller.DistributionSemenCtlrImpl;
import org.gov.vis.data.dto.DistributionSemenDto;
import org.gov.vis.data.gui.DistributionSemenDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * 
 * @author user
 *
 */
public class DistributionSemenListener implements ActionListener,BaseListener {



	private DistributionSemenDialog distributionSemenDailog;
	private DistributionSemenCtlr distributionSemenCtlr = new DistributionSemenCtlrImpl();
	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private DistributionSemenDto distributionSemenDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public DistributionSemenListener(DistributionSemenDialog distributionSemensDialog){

		this.distributionSemenDailog = distributionSemensDialog;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		try{
			errorMsgPanel = new ErrorsDisplayJPanel(distributionSemenDailog.getjPanel1());
			errorMsgPanel.clearMsgPanel();

			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the GopalaMithra details
				addOrEditDistributionSemen();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){			
				//Method called to view the details of DistributionSemen Object based on the passed distributionSemenId
				viewDistributionSemen(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the DistributionSemen Object based on the passed distributionSemenId.
				deleteDistributionSemen(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}//end actionPerformed


	private void viewDistributionSemen(final long _distributionSemenId) {
		// TODO Auto-generated method stub
		try{
			final DistributionSemenDto _distributionSemenDto = distributionSemenCtlr.getDisSemen(_distributionSemenId);
			setDtoDetailsToGui(_distributionSemenDto);
			distributionSemenDailog.validate();
			distributionSemenDailog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * Method called to either Add or Edit the GopalaMithra details
	 */
	private void addOrEditDistributionSemen(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(distributionSemenDailog);

		try{
			//Method called to Add or Edit the GopalaMithra
			distributionSemenCtlr.addOrEditDisSemen(distributionSemenDto);
			/*List<String> successMsgLst = new ArrayList<String>();
			successMsgLst.add(rb.getString(GuiConstants.DATA_ADD_DISTRBUTIONSEMEN_SUCCESS));
			errorMsgPanel.setSuccessMessages(successMsgLst);
			*/
			JOptionPane.showMessageDialog(distributionSemenDailog, rb.getString(GuiConstants.DATA_ADD_DISTRBUTIONSEMEN_SUCCESS));
			//distributionSemenDailog.dispose();
			

			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = DistributionSemenCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_dist_semen_code").toUpperCase())){
						distributionSemenDailog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_semen_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_dist_semen_nostrwas").toUpperCase())){
						distributionSemenDailog.getLblNoOfStraws().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_semen_nostrwas").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_dist_semen_dtofdistr").toUpperCase())){
						distributionSemenDailog.getLblDODistribution().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_semen_dtofdistr").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_dist_semen_bullno").toUpperCase())){
						distributionSemenDailog.getLblBullNo().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_semen_bullno").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					errorMsgPanel.setErrorMessages(errorMsgLst);					
				} else {
					//log.debug(e.getMessage(), e);
				}
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);
			}
		}		
	}//end addOrEditGopalaMithra

	/**
	 * Method called to delete the DistributionSemen Object based on the passed distributionSemenId
	 * @param _distributionSemenId
	 */
	private void deleteDistributionSemen(final long  _distributionSemenId){
		try{
			distributionSemenCtlr.deleteDisSemen(_distributionSemenId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteDistributionSemen

	/**
	 * Method called to clear the screen data
	 */
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		distributionSemenDailog.getTxtFldDSBreed().setText("");
		distributionSemenDailog.getTxtFldDSBullNo().setText("");
		distributionSemenDailog.getTxtFldDSCode().setText("");
		distributionSemenDailog.getTxtFldDSDiscord().setText("");
		distributionSemenDailog.getDtChoDateOfDistribution().setDate(null);
		distributionSemenDailog.getTxtFldDSNoOfStraws().setText("");
		distributionSemenDailog.getTxtFldDSValue().setText("");

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);


	}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		final DistributionSemenDto _distributionSemenDto = (DistributionSemenDto)object;
		distributionSemenDailog.getTxtFldDSBreed().setText(_distributionSemenDto.getBreed());
		distributionSemenDailog.getTxtFldDSBullNo().setText(_distributionSemenDto.getBullNo());
		distributionSemenDailog.getTxtFldDSCode().setText(_distributionSemenDto.getCode());
		distributionSemenDailog.getTxtFldDSDiscord().setText(_distributionSemenDto.getDiscord());
		distributionSemenDailog.getTxtFldDSDODistribution().setText(DU.format(_distributionSemenDto.getDoDstrbut(),DBConstants.DISPLAY_DATE_FORMAT));
		distributionSemenDailog.getTxtFldDSNoOfStraws().setText(_distributionSemenDto.getNoStrawas());
		distributionSemenDailog.getTxtFldDSValue().setText(_distributionSemenDto.getValue());

	}//end setDtoDetailsToGui

	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		final DistributionSemenDialog _distributionSemenDialog = (DistributionSemenDialog)object;
		distributionSemenDto = new DistributionSemenDto();
		distributionSemenDto.setBreed(_distributionSemenDialog.getTxtFldDSBreed().getText());
		distributionSemenDto.setBullNo(_distributionSemenDialog.getTxtFldDSBullNo().getText());
		distributionSemenDto.setCode(_distributionSemenDialog.getTxtFldDSCode().getText());
		distributionSemenDto.setDiscord(_distributionSemenDialog.getTxtFldDSDiscord().getText());
		//distributionSemenDto.setDoDist(DU.parse(_distributionSemenDialog.getTxtFldDSDODistribution().getText()));
		if(_distributionSemenDialog.getDtChoDateOfDistribution().getDate() != null){
			distributionSemenDto.setDoDstrbut(_distributionSemenDialog.getDtChoDateOfDistribution().getDate());
			}

		distributionSemenDto.setNoStrawas(_distributionSemenDialog.getTxtFldDSNoOfStraws().getText());
		distributionSemenDto.setValue(_distributionSemenDialog.getTxtFldDSValue().getText());

	}//end setGuiDetailsToDto

	public void setDistributionSemensDailog(final DistributionSemenDialog _distrbutionSemenDailog) {
		this.distributionSemenDailog = _distrbutionSemenDailog;
	}

	public DistributionSemenDialog getGopalaMithrasDailog() {
		return distributionSemenDailog;
	}

	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		distributionSemenDailog.getLblBreed().setForeground(_lblColor);
		distributionSemenDailog.getLblBullNo().setForeground(_lblColor);
		distributionSemenDailog.getLblCode().setForeground(_lblColor);
		distributionSemenDailog.getLblDiscord().setForeground(_lblColor);
		distributionSemenDailog.getLblDODistribution().setForeground(_lblColor);
		distributionSemenDailog.getLblNoOfStraws().setForeground(_lblColor);
		distributionSemenDailog.getLblValue().setForeground(_lblColor);


	}


}//end class
